// mircoApp/store-oil-station/InquiryResults/InquiryResults.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    coupon_list: {},
    license_plate: {},
    member: {},
    order_list: {},
    uid: '',
    show_limit: null,
    checkLimit: false,

    isLoadMore: true,
    page: 1,
    total_page: 0,
    love_name: "",
    credit: "",
    credit1: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getData();
    let basic_info = wx.getStorageSync('yz_basic_info');
    this.setData({
      love_name: basic_info.love,
      credit1: basic_info.lang.member_center.credit1,
      credit: basic_info.lang.member_center.credit
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(this.data.isLoadMore, 'wwwwwwww')
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
        uid: this.data.member.uid
      })
      let utlStr = "";
      if (this.data.active == 0) {
        utlStr = app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.get-list");
      } else {
        utlStr = app.getNetAddresss("plugin.store-oil-station.frontend.license-plate.get-oil-order")
      }
      let {
        data
      } = await app._postNetWork({
        url: utlStr,
        data: {
          page: this.data.page,
          uid: this.data.member.uid
        }
      })
      this.data.isLoadMore = true;
      if (data.result) {
        if (this.data.active == 0) {
          this.setData({
            coupon_list: this.data.coupon_list.concat(data.data.list.data)
          })
        } else if (this.data.active == 1) {
          this.setData({
            order_list: this.data.order_list.concat(data.data.data)
          })
        }
      } else {
        app.tips(data.msg);
        this.data.isLoadMore = false;
      }
    }
  },
  tabChange(e) {
    this.setData({
      active: e.detail.name,
      page: 1,
      total_page: 0,
      isLoadMore: true,
      coupon_list: [],
      order_list: []
    })
    if (this.data.active == 1) {
      this.getOrderList(this.data.member.uid);
    } else {
      this.getCouponList(this.data.member.uid);
    }
  },
  toSearch() {
    wx.redirectTo({
      url: '/mircoApp/store-oil-station/numberplateInquiry/numberplateInquiry',
    })
  },
  async getOrderList(id) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.license-plate.get-oil-order"),
      data: {
        uid: id
      }
    })
    if (data.result) {
      this.setData({
        order_list: data.data.data,
        total_page: data.data.last_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg);
    }
  },
  async getCouponList(id) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.get-list"),
      data: {
        uid: id
      }
    })
    if (data.result) {
      this.setData({
        coupon_list: data.data.list.data,
        total_page: data.data.list.last_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let json = {
      plate_num: this.options.card_num
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.store-oil-station.frontend.license-plate.index"),
      data: json
    })
    if (data.result) {
      this.setData({
        coupon_list: data.data.coupon_list?.data || null,
        license_plate: data.data.license_plate,
        member: data.data.member,
        show_limit: data.data.show_limit,
      })
      let checkLimit = false;
      for (const key in this.data.show_limit) {
        if (this.data.show_limit[key] == 1) {
          checkLimit = true;
        }
      }
      this.setData({
        checkLimit: checkLimit
      })
      this.getCouponList(this.data.member.uid)
      console.log(this.data.active)
    } else {
      app.tips(data.msg)
    }
  }
})